var postsData = require('../../../data/posts-data.js')

Page({

  data: {
    isPlayingMusic: false
  },

  onLoad: function (option) {
    var postId = option.id
    var postData = postsData.postList[postId]
    this.setData({
      currentPostId: postId,
      postData: postData
    })
    var postsCollected = wx.getStorageSync("posts_collected")
    if (postsCollected) {
      this.setData({
        collected: postsCollected[postId]
      })
    } else {
      var postsCollected = {}
      postsCollected[postId] = false
      wx.setStorageSync("posts_collected", postsCollected)
    }
  },

  onCollectionTap: function (event) {
    var postsCollected = wx.getStorageSync("posts_collected")
    var postCollected = postsCollected[this.data.currentPostId]
    postCollected = !postCollected
    postsCollected[this.data.currentPostId] = postCollected
    this.showToast(postsCollected, postCollected)
  },

  showToast: function (postsCollected, postCollected) {
    wx.setStorageSync("posts_collected", postsCollected)
    this.setData({
      collected: postCollected
    })
    wx.showToast({
      title: postCollected ? "收藏成功" : "取消成功"
    })
  },

  onShareTap: function () {
    wx.showActionSheet({
      itemList: [
        "分享给微信好友",
        "分享到朋友圈"
      ],
      itemColor: "#405f80",
      success: function (res) {

      }
    })
  },

  onMusicTap: function (event) {
    var currentPostId = this.data.currentPostId
    var postData = postsData.postList[currentPostId]
    if (this.data.isPlayingMusic) {
      wx.pauseBackgroundAudio()
    } else {
      wx.playBackgroundAudio({
        dataUrl: postData.music.url,
        title: postData.music.title,
        coverImgUrl: postData.music.coverImg
      })
    }
    this.setData({
      isPlayingMusic: !this.data.isPlayingMusic
    })
  }

})